package db;

import Info.SystemEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.Car;
import pojo.Contrast;
import pojo.User;

import java.util.Map;

public class ManageContrastDB {
    private Map<User, Contrast> contrastMap;
    private Log log;
    

    public ManageContrastDB(Log log) {
        contrastMap = DataBase.getInstance().getContrastsMap();
        this.log = log;
    }

    public ManageContrastDB(Map<User, Contrast> contrastMap) {
        this.contrastMap = contrastMap;
        log = LogFactory.getLog(getClass());
    }
    //查找是否存在该键
    public boolean find(User user){
        if(user==null) return false;
        return contrastMap.containsKey(user);
    }

    //查找是否存在该值
    public boolean find(User user, Car car){
        if(find(user)){
            Contrast contrast = contrastMap.get(user);
            return contrast.setFind(car);
        }
        return false;
    }

    //增加键值对--键
    public boolean add(User user){
        if(find(user)){
            log.info(getClass()+"\tcontrast-add-k\t"+ SystemEnum.ERROR_REPEAT.toString());
            return false;
        }else{
            Contrast contrast = new Contrast();
            contrastMap.put(user, contrast);
            log.info(getClass()+"\tcontrast-add-k\t"+ SystemEnum.SUCCESS.toString());
            return true;
        }
    }
    //增加值
    public boolean add(User user,Car car){
        if(find(user)){
            Contrast contrast = contrastMap.get(user);
            contrast.setAdd(car);
        }else {
            add(user);
            add(user,car);
        }
        log.info(getClass()+"\tcontrast-add-v\t"+ SystemEnum.SUCCESS.toString());
        return true;
    }

    //删-k
    public boolean delete(User user){
        if(find(user)){
            contrastMap.remove(user);
            log.info(getClass()+"\tcontrast-delete-k\t"+ SystemEnum.SUCCESS.toString());
            return true;
        }else{
            log.info(getClass()+"\tcontrast-delete-k\t"+ SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    //删-v
    public boolean delete(User user,Car car){
        if(find(user,car)){
            Contrast contrast = contrastMap.get(user);
            log.info(getClass()+"\tcontrast-delete-v\t"+ SystemEnum.SUCCESS.toString());
            return contrast.setDelete(car);
        }
        log.info(getClass()+"\tcontrast-delete-v\t"+ SystemEnum.ERROR_MATCH.toString());
        return false;
    }
    public void printAUserContrast(User user){
        Contrast contrast = contrastMap.get(user);
        if(contrast!=null){
            log.info("user : "+user.toString());
            log.info(contrast);
        }else log.info("该用户对比集合为空");
    }

    public void printAll(){
        System.out.println(contrastMap);
    }

    @Override
    public String toString() {
        return "ManageContrastDB{" +
                "contrastMap=" + contrastMap +
                '}';
    }
   public Contrast getAUserContrast(User user){
        if(find(user)) return contrastMap.get(user);
        else return null;
   }
}
